package com.testes.umov.util;

import java.io.Serializable;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;

public class BatteryHelper implements Serializable{
		/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		private float inicioPercentual = 0;
		private float inicio = 0;
		private float gastoPercentual = 0;
		private float gasto = 0;
		private float ultimoNivelPercentual = 0;
		private float nivelPercentual = 0;
		private Context contexto;
		public static boolean bCriado = false;
		private BatteryHelperListener listener = null;
		
		//Constructor da Classe
	   public BatteryHelper (Context c) {
		    contexto = c;
	        inicioPercentual = 0;
	        inicio = 0;
	       	//iniciarMedicao();
	       	bCriado = true;
	   }
	   
	   public float getInicioPercentual() {
		return inicioPercentual;
	}

	public void setInicioPercentual(float inicioPercentual) {
		this.inicioPercentual = inicioPercentual;
	}

	public float getNivelPercentual() {
		return nivelPercentual;
	}

	public void setNivelPercentual(float nivelPercentual) {
		this.nivelPercentual = nivelPercentual;
	}

	public void setGastoPercentual(float gastoPercentual) {
		this.gastoPercentual = gastoPercentual;
	}

	public void setListener(BatteryHelperListener listener) {
		this.listener = listener;
	}
	
	public BatteryHelper (Context c, BatteryHelperListener _listener) {
		   listener = _listener;
		   contexto = c;
	        inicioPercentual = 0;
	        inicio = 0;
	       	//iniciarMedicao();
	       	bCriado = true;
	   }
	   
	   
	   public void reiniciarMedicao(){
		   inicioPercentual = 0;
		   inicio = 0;
	   }
	   
	   public float getGastoPercentual(){
		   return gastoPercentual;
	   }
	   
	   public float getGasto(){
		   return gasto;
	   }
	   
	public void iniciarMedicao() {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
					@Override
					public void onReceive(Context context, Intent intent) {
						try {
							context.unregisterReceiver(this);
							float nivel = (float)intent.getIntExtra("level", -1);
							float nivelMaximo = (float)intent.getIntExtra("scale", -1);
							float percentual = -1;
							
							if (nivel >= 0 && nivelMaximo > 0) {
								percentual = (nivel * 100) / nivelMaximo;
								nivelPercentual = percentual;
							}
							
							if(inicioPercentual == 0) {
								inicioPercentual = percentual;
							}
							
							if(inicio == 0) {
								inicio = nivel;
							}
							
							gastoPercentual = inicioPercentual - percentual;
							gasto = inicio - nivel;
							
							if(listener != null) {
								
								if(ultimoNivelPercentual != nivelPercentual) {								
									listener.onBatteryLevelChanged(nivelPercentual, inicioPercentual, gastoPercentual);	
								}
								ultimoNivelPercentual = nivelPercentual;
							}
							
							iniciarMedicao();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							iniciarMedicao();
						}
					}
			    };
			    IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
			    contexto.registerReceiver(batteryLevelReceiver, batteryLevelFilter);
			}
		}).start();
		
	}

	public float getNivel() {
		return this.nivelPercentual;
	}
	
}
